package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/5/18
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    DtsGoodsMapper goodsMapper;

    @Override
    public Integer getTotal() {
        return goodsMapper.selectCount(null);
    }

    @Override
    public IPage<DtsGoods> selectPage(Integer page, Integer limit, String goodsSn, String name, String sort, String order, List<Integer> brandIds) {
        IPage<DtsGoods> iPage = new Page<>(page, limit);
        QueryWrapper<DtsGoods> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsSn)){
            qw.like("goods_sn", goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            qw.like("name", name);
        }

        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if("desc".equalsIgnoreCase(order)){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        if(!CollectionUtils.isEmpty(brandIds)){
            qw.in("brand_id",brandIds);
        }
        goodsMapper.selectPage(iPage, qw);
        return iPage;
    }

    @Autowired
    DtsGoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    DtsGoodsProductMapper goodsProductMapper;

    @Autowired
    DtsGoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    DtsCategoryMapper categoryMapper;
    @Override
    public Map<String, Object> queryDetailById(Integer id) {
        DtsGoods dtsGoods = goodsMapper.selectById(id);
        LambdaQueryWrapper<DtsGoodsAttribute> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DtsGoodsAttribute::getGoodsId, id);
        qw1.eq(DtsGoodsAttribute::getDeleted, false);
        List<DtsGoodsAttribute> dtsGoodsAttributes = goodsAttributeMapper.selectList(qw1);
        LambdaQueryWrapper<DtsGoodsProduct> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(DtsGoodsProduct::getGoodsId, id);
        qw2.eq(DtsGoodsProduct::getDeleted, false);
        List<DtsGoodsProduct> dtsGoodsProducts = goodsProductMapper.selectList(qw2);
        LambdaQueryWrapper<DtsGoodsSpecification> qw3 = new LambdaQueryWrapper<>();
        qw3.eq(DtsGoodsSpecification::getGoodsId, id);
        qw3.eq(DtsGoodsSpecification::getDeleted, false);
        List<DtsGoodsSpecification> dtsGoodsSpecifications = goodsSpecificationMapper.selectList(qw3);
        Integer categoryId2 = dtsGoods.getCategoryId();
//        LambdaQueryWrapper<DtsCategory> qw4 = new LambdaQueryWrapper<>();
//        qw4.eq(DtsCategory::getId, categoryId2);
//        qw4.select(DtsCategory::getPid);
        DtsCategory dtsCategory = categoryMapper.selectById(categoryId2);
        Integer categoryId1 = dtsCategory.getPid();
//        List<Integer> list = new ArrayList<>();
//        list.add(categoryId1);
//        list.add(categoryId2);
        Map<String,Object> data = new HashMap<>();
        data.put("categoryIds", Arrays.asList(categoryId1,categoryId2));
        data.put("goods", dtsGoods);
        data.put("attributes",dtsGoodsAttributes );
        data.put("specifications",dtsGoodsSpecifications );
        data.put("products", dtsGoodsProducts);
        return data;
    }

    @Autowired
    DtsGoodsAttributeService attributeService;
    @Autowired
    DtsGoodsSpecificationService specificationService;
    @Autowired
    DtsGoodsProductService goodsProductService;
    @Autowired
    DtsBrandMapper brandMapper;
    @Override
    public Object update(GoodsAllinone goodsAllinone) {
        Object result = this.checkParam(goodsAllinone);

        if(result!=null){
            return result;
        }
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goodsMapper.updateById(goods);

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        goodsProductService.uddateProducts(goods.getId(),products);

        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        specificationService.updateSpecification(goods.getId(),specifications);

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        attributeService.updateAttr(goods.getId(),attributes);

        return  ResponseUtil.ok();
    }

    @Override
    public Object checkParam(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        if(goods==null){
            return ResponseUtil.badArgument();
        }
        if(StringUtils.isEmpty(goods.getName())){
            return AdminResponseUtil.fail(-1,"商品名称不能为空");
        }
        if(StringUtils.isEmpty(goods.getGoodsSn())){
            return AdminResponseUtil.fail(-1,"商品编号不能为空");
        }
        Integer brandId = goods.getBrandId();

        DtsBrand dtsBrand = brandMapper.selectById(brandId);
        if(dtsBrand==null){
            return AdminResponseUtil.fail(-1,"品牌商不存在");
        }
        Integer categoryId = goods.getCategoryId();
        DtsCategory dtsCategory = categoryMapper.selectById(categoryId);
        if(dtsCategory==null){
            return AdminResponseUtil.fail(-1,"分类不存在");
        }
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
//        if(CollectionUtils.isEmpty(Arrays.asList(attributes))){
//
//        }
        if(attributes==null&&attributes.length==0){
            return AdminResponseUtil.fail(-1,"商品属性不能为空");
        }
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products==null&&products.length==0){
            return AdminResponseUtil.fail(-1,"商品库存不能为空");
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications==null&&specifications.length==0){
            return AdminResponseUtil.fail(-1,"商品规格不能为空");
        }
        return null;
    }

    @Override
    public Object Insert(GoodsAllinone goodsAllinone) {
        //1. 校验参数
        Object result = this.checkParam(goodsAllinone);

        if(result!=null){
            return result;
        }
        //2. 根据商品名字判断此商品是否已售卖
        //2.1如果已售卖，则无法添加
        //2.2如果未售卖，则可以添加
        DtsGoods goods = goodsAllinone.getGoods();
        LambdaQueryWrapper<DtsGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getName, goods.getName());
        DtsGoods dtsGoods = goodsMapper.selectOne(qw);
        if(dtsGoods!=null){
            return  ResponseUtil.fail(-1, "商品已存在，无法重复添加");

        }
        //3. 添加商品SPU, 商品基本信息表Dts_goods
        goods.setUpdateTime(new Date());
        goods.setAddTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insert(goods);
        //4. 添加商品规格数据, 商品规格表Dts_goods_specification
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        specificationService.insert(goods.getId(),specifications);

        //5. 添加商品参数数据, 商品参数表Dts_goods_attribute
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        attributeService.insert(goods.getId(),attributes);
        //6. 添加商品库存数据SKU, 商品库存表Dts_product
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        goodsProductService.insert(goods.getId(),products);

        return ResponseUtil.ok();


    }

    @Override
    public Object delete(DtsGoods goods) {
//        //1. 根据商品id删除商品数据SPU
//        goodsMapper.deleteById(goods.getId());
//        //2. 根据商品id删除商品规格数据
//        specificationService.delete(goods);
//        //3. 根据商品id删除商品属性数据
//        attributeService.delete(goods);
//        //4. 根据商品id删除商品库存数据SKU
//        goodsProductService.delete(goods);
        long count = goodsMapper.selectOrderCountById(goods.getId());
        if(count>0){
            return ResponseUtil.fail(-2, "该商品正在热卖");
        }
        goods.setDeleted(true);
        goodsMapper.updateById(goods);
        specificationService.deleteByGoodsId(goods);
        attributeService.deleteByGoodsId(goods);
        goodsProductService.deleteByGoodsId(goods);
        return ResponseUtil.ok();
    }

    @Override
    public void deleteByBrand(DtsBrand brand) {
        //先删除商品
        LambdaQueryWrapper<DtsGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getBrandId,brand.getId());
        goodsMapper.delete(qw);
        List<DtsGoods> dtsGoods = goodsMapper.selectList(qw);
//        //2. 根据商品id删除商品规格数据
//        specificationService.delete(goods);
//        //3. 根据商品id删除商品属性数据
//        attributeService.delete(goods);
//        //4. 根据商品id删除商品库存数据SKU
//        goodsProductService.delete(goods);
        for (DtsGoods dtsGood : dtsGoods) {
            specificationService.delete(dtsGood);
            attributeService.delete(dtsGood);
            goodsProductService.delete(dtsGood);
        }


    }

    @Override
    public List<DtsGoods> selectByBrand(DtsBrand brand) {
        LambdaQueryWrapper<DtsGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getBrandId,brand.getId() );
        List<DtsGoods> dtsGoods = goodsMapper.selectList(qw);
        return dtsGoods;
    }
}
